# menustate.rb
# created by: Dan Alexander
# date: 3/6/2011
# license: New BSD License
#
# MenuState
# The menu state, inherited from gamestate.
# This handles all of the main menus.

require "rubygems"
require "rubygame"

include Rubygame

class MenuGameState < GameState

	def initialize(scr)
		super(scr) #GameState parent class initialize
		
		#set up the fonts
		TTF.setup
		@fontTitle = TTF.new "resources/font.ttf", 28
		@fontItem = TTF.new "resources/font.ttf", 22
		
		#colors used for menu text
		@colorTitle = [255,255,0]
		@colorMenu = [128,128,128]
		@colorMenuSelected = [255,255,255]
		
		@selected = 0
	end
	
	def update
		$event_queue.each do |event|
			case event
				when Events::KeyReleased
					if event.key == :escape
						if $debug == true then p "quitting game..." end
						$should_run = false
					elsif event.key == :up #navigate through menu
						@selected==0 ? @selected=2 : @selected -= 1
					elsif event.key == :down #navigate through menu
						@selected==2 ? @selected=0 : @selected += 1
					elsif event.key == :return #do action on currently selected menu item
						if @selected == 0
							# join game
							if $gamestates["ingame"].server_running == false
								$gamestates["ingame"].server_start
								if $gamestates["ingame"].server_running == true
									$currentstate = "charselect"
								else
									$gamestates["message"].show "Sorry, could not connect to server."
								end
							else
								$currentstate = "charselect"
							end
							
						elsif @selected == 1
							# create game (host)
							#not currently implemented
							$gamestates["message"].show "Currently not implemented."
						elsif @selected == 2
							#quit the game
							$should_run = false
						end
					end				
					
				when Events::QuitRequested
					$should_run = false
			end
		end
  
	end
	
	def draw
		#fill the screen with a background color
		@screen.fill([0,0,0],[0,0,@screen.w(),@screen.h()])
		
		text_surface = @fontTitle.render_utf8 "Adventures of 5 Nachos Awesomeness", true, @colorTitle
		rt = text_surface.make_rect
		rt.topleft = [@screen.width / 2 - rt.width / 2, 8]
		text_surface.blit @screen, rt
		
		#JOIN GAME MENU ITEM
		text_surface = @fontItem.render_utf8 "Join the Awesomeness", true, @selected==0 ? @colorMenuSelected : @colorMenu
		rt = text_surface.make_rect
		rt.topleft = [@screen.width / 2 - rt.width / 2, 100]
		text_surface.blit @screen, rt
		
		#HOST GAME MENU ITEM
		text_surface = @fontItem.render_utf8 "Create Your Own Awesomeness", true, @selected==1 ? @colorMenuSelected : @colorMenu
		rt = text_surface.make_rect
		rt.topleft = [@screen.width / 2 - rt.width / 2, 130]
		text_surface.blit @screen, rt
		
		#QUIT MENU ITEM
		text_surface = @fontItem.render_utf8 "Quit being Awesome", true, @selected==2 ? @colorMenuSelected : @colorMenu
		rt = text_surface.make_rect
		rt.topleft = [@screen.width / 2 - rt.width / 2, 160]
		text_surface.blit @screen, rt
		
	end
	
end